package cn.dkdoo.utils.templateDefault.es;

import cn.dkdoo.utils.config.rollbackData.RollbackDataSelf;
import cn.dkdoo.utils.config.transactionInfo.TransactionInfoAbstract;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Nicky.Tang on 2021/5/13 11:35 下午
 *
 * @since 02.12.10
 */

public class DefaultEsTemplate extends RestHighLevelClient {
    public DefaultEsTemplate(RestClientBuilder restClientBuilder) {
        super(restClientBuilder);
    }

    public BulkResponse bulkDkd(BulkRequest bulkRequest, RequestOptions options) throws IOException {
        List<ActionRequest> requests = bulkRequest.requests();
        for (ActionRequest request : requests) {
            //DocWriteRequest docWriteRequest = request;
            String beanName = "";
            String methodName = "";
            Collection<?> args = new ArrayList<>();
            RollbackDataSelf rollbackDataSelf = new RollbackDataSelf(beanName, methodName, args);

            if (request instanceof DeleteRequest) {

            } else if (request instanceof UpdateRequest) {

            }

            TransactionInfoAbstract.addRollbackDataStatic(rollbackDataSelf);
        }
        BulkResponse responses = super.bulk(bulkRequest, options);
        return responses;
    }
}
